﻿namespace CityProvince.Model;

/// <summary>
/// 确保 <see cref="ConstantCollections"/> 加载完毕
/// </summary>
public class CityItem : DependencyObject {
    public static readonly DependencyProperty CityProperty = DependencyProperty.Register("City", typeof(string), typeof(CityItem), new PropertyMetadata(string.Empty));
    public static readonly DependencyProperty SelectedProvinceProperty = DependencyProperty.Register("SelectedProvince", typeof(string), typeof(CityItem), new PropertyMetadata(string.Empty, SelectedProvincePropertyChangedHandler));
    public static readonly DependencyProperty IconProperty = DependencyProperty.Register("Icon", typeof(string), typeof(CityItem), new PropertyMetadata(string.Empty));
    public static readonly DependencyProperty IconColorProperty = DependencyProperty.Register("IconColor", typeof(SolidColorBrush), typeof(CityItem), new PropertyMetadata(new SolidColorBrush(Colors.Transparent)));

    public const string CorrectIcon = "\ue641";
    public const string InCorrectIcon = "\ue6c6";
    public static readonly SolidColorBrush InCorrectColorBrush = new(Color.FromArgb(255, 207, 55, 54));
    public static readonly SolidColorBrush CorrectColorBrush = new(Color.FromArgb(255, 57, 181, 77));

    /// <summary>
    /// 城市
    /// </summary>
    public string City {
        get { return (string)GetValue(CityProperty); }
        set { SetValue(CityProperty, value); }
    }
    /// <summary>
    /// 所选省份
    /// </summary>
    public string SelectedProvince {
        get { return (string)GetValue(SelectedProvinceProperty); }
        set { SetValue(SelectedProvinceProperty, value); }
    }
    /// <summary>
    /// Icon
    /// </summary>
    public string Icon {
        get { return (string)GetValue(IconProperty); }
        set { SetValue(IconProperty, value); }
    }
    /// <summary>
    /// IconColor
    /// </summary>
    public SolidColorBrush IconColor {
        get { return (SolidColorBrush)GetValue(IconColorProperty); }
        set { SetValue(IconColorProperty, value); }
    }

    public CityItem(string city) {
        City = city;
    }

    private static void SelectedProvincePropertyChangedHandler(DependencyObject d, DependencyPropertyChangedEventArgs e) {
        if (d is not CityItem self) {
            return;
        }
        if (self.SelectedProvince == null) {
            return;
        }
        if (!ConstantCollections.CityProvinceDict.TryGetValue(self.City, out var prov)) {
            return;
        }
        self.Icon = prov == self.SelectedProvince ? CorrectIcon : InCorrectIcon;
        self.IconColor = prov == self.SelectedProvince ? CorrectColorBrush : InCorrectColorBrush;
    }
}